package com.ruoyi.combat.service.useralignment.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.domain.useralignment.*;
import com.ruoyi.combat.mapper.useralignment.*;
import com.ruoyi.combat.service.externalServiceInvoker.NetworkUserIdentityProtectionService;
import com.ruoyi.combat.service.useralignment.ICombatUserWbService;
import com.ruoyi.combat.service.useralignment.ICombatUserZhService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.service.useralignment.ICombatHeavyUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 重保用户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-14
 */
@Service
public class CombatHeavyUserServiceImpl implements ICombatHeavyUserService {
    @Autowired
    private CombatHeavyUserMapper combatHeavyUserMapper;
    @Autowired
    private NetworkUserIdentityProtectionService networkUserIdentityProtectionService;
    @Autowired
    private CombatUserWbMapper combatUserWbMapper;
    @Autowired
    private CombatRelationWbMapper combatRelationWbMapper;
    @Autowired
    private CombatUserZhMapper combatUserZhMapper;
    @Autowired
    private CombatRelationZhMapper combatRelationZhMapper;
    @Autowired
    private CombatAlignmentWarnMapper combatAlignmentWarnMapper;
    @Autowired
    private ICombatUserWbService combatUserWbService;
    @Autowired
    private ICombatUserZhService combatUserZhService;

    /**
     * 查询重保用户
     *
     * @param id 重保用户主键
     * @return 重保用户
     */
    @Override
    public CombatHeavyUser selectCombatHeavyUserById(Long id) {
        return combatHeavyUserMapper.selectCombatHeavyUserById(id);
    }

    /**
     * 查询重保用户列表
     *
     * @param combatHeavyUser 重保用户
     * @return 重保用户
     */
    @Override
    public List<CombatHeavyUser> selectCombatHeavyUserList(CombatHeavyUser combatHeavyUser) {
        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(combatHeavyUser);
        for (CombatHeavyUser combatHeavyUser1 : combatHeavyUsers) {
            try {
                String wbUserId = combatHeavyUser1.getWbUserId();
                String zhUserId = combatHeavyUser1.getZhUserId();
                CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(wbUserId);
                CombatUserZh combatUserZh = combatUserZhMapper.selectCombatUserZhByUserId(zhUserId);
                if (combatUserWb != null) {
                    combatHeavyUser1.setWbUserName(combatUserWb.getUserName());
                }
                if (combatUserZh != null) {
                    combatHeavyUser1.setZhUserName(combatUserZh.getUserName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return combatHeavyUsers;
    }

    /**
     * 新增重保用户
     *
     * @param combatHeavyUser 重保用户
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCombatHeavyUser(CombatHeavyUser combatHeavyUser) throws IOException {
        String methodName = combatHeavyUser.getMethodName();
        if (StringUtils.isEmpty(methodName)) {
            methodName = "BigAlign";
        }
        String wbUserId = combatHeavyUser.getWbUserId();
        if (wbUserId == null) {
            throw new RuntimeException("微博用戶Id不能為空");
        }
        String zhUserId = combatHeavyUser.getZhUserId();
        if (zhUserId == null) {
            throw new RuntimeException("知乎用戶Id不能為空");
        }

        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(combatHeavyUser);
        if (combatHeavyUsers.size() > 0) {
            throw new RuntimeException("用户已存在");
        }
        combatHeavyUser.setCreateTime(DateUtils.getNowDate());
        int i = combatHeavyUserMapper.insertCombatHeavyUser(combatHeavyUser);
        checkHeavyUser(combatHeavyUser);
        return i;
    }


    /**
     * 检测用户信息是否存在风险
     */
    public void checkHeavyUserList() {
        CombatHeavyUser combatHeavyUser1 = new CombatHeavyUser();
        combatHeavyUser1.setStatus("0");
        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(combatHeavyUser1);
        for (CombatHeavyUser combatHeavyUser : combatHeavyUsers) {
            checkHeavyUser(combatHeavyUser);
        }
    }

    public void checkHeavyUser(CombatHeavyUser combatHeavyUser) {
        try {
            String wbUserId = combatHeavyUser.getWbUserId();

            String zhUserId = combatHeavyUser.getZhUserId();
            String methodName = combatHeavyUser.getMethodName();
            if (StringUtils.isEmpty(methodName)) {
                methodName = "BigAlign";
            }

            CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(wbUserId);
            Map<String, Object> postParamMap = new HashMap<>();
            postParamMap.put("Attributes", combatUserWb);
            String wbs = networkUserIdentityProtectionService.checkFunction(postParamMap);
            combatUserWb.setScore(wbs);
            combatUserWbMapper.updateCombatUserWb(combatUserWb);

            CombatUserZh combatUserZh = combatUserZhMapper.selectCombatUserZhByUserId(zhUserId);
            postParamMap.put("Attributes", combatUserZh);
            String zhs = networkUserIdentityProtectionService.checkFunction(postParamMap);
            combatUserZh.setScore(zhs);
            combatUserZhMapper.updateCombatUserZh(combatUserZh);


            Double v = Double.valueOf(wbs);
//        if (v > 0.2 && v < 0.4) {
            if (v > 0.5) {
                CombatRelationWb combatRelationWb = new CombatRelationWb();
                combatRelationWb.setUserid(wbUserId);
                List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
                int size = combatRelationWbs.size();
                if(size>30){
                    combatRelationWbs = combatRelationWbs.subList(0,30);
                }

                CombatRelationZh combatRelationZh = new CombatRelationZh();
                combatRelationZh.setUserid(zhUserId);
                List<CombatRelationZh> combatRelationZhs = combatRelationZhMapper.selectCombatRelationZhList(combatRelationZh);

                //添加风险警告
                CombatAlignmentWarn combatAlignmentWarn = new CombatAlignmentWarn();
                combatAlignmentWarn.setUserFromId(wbUserId);
                combatAlignmentWarn.setUserFromName(combatUserWb.getUserName());
                combatAlignmentWarn.setUserFromPlate("微博");
                combatAlignmentWarn.setUserToId(zhUserId);
                combatAlignmentWarn.setUserToName(combatUserZh.getUserName());
                combatAlignmentWarn.setUserToPlate("知乎");
                combatAlignmentWarn.setStatus("0");
                combatAlignmentWarn.setMethodId(32l);
                combatAlignmentWarn.setMethodName(methodName);
                combatAlignmentWarn.setUserToGroup("身份对齐");
                combatAlignmentWarn.setAligntime(DateUtils.getNowDate());
                combatAlignmentWarnMapper.insertCombatAlignmentWarn(combatAlignmentWarn);
                Long id = combatAlignmentWarn.getId();
                // 调用对齐算法
                networkUserIdentityProtectionService.aligning(methodName, combatRelationWbs, combatRelationZhs, wbUserId, zhUserId, id);
            }
            combatHeavyUser.setStatus("1");
            updateCombatHeavyUser(combatHeavyUser);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 修改重保用户
     *
     * @param combatHeavyUser 重保用户
     * @return 结果
     */
    @Override
    public int updateCombatHeavyUser(CombatHeavyUser combatHeavyUser) {
        combatHeavyUser.setUpdateTime(DateUtils.getNowDate());
        return combatHeavyUserMapper.updateCombatHeavyUser(combatHeavyUser);
    }

    /**
     * 批量删除重保用户
     *
     * @param ids 需要删除的重保用户主键
     * @return 结果
     */
    @Override
    public int deleteCombatHeavyUserByIds(Long[] ids) {
        return combatHeavyUserMapper.deleteCombatHeavyUserByIds(ids);
    }

    @Override
    public int detect(Long[] ids) {
        for (Long id : ids) {
            CombatHeavyUser combatHeavyUser = combatHeavyUserMapper.selectCombatHeavyUserById(id);
            checkHeavyUser(combatHeavyUser);
        }
        return 0;
    }

    @Override
    public int detectAll() {
        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(new CombatHeavyUser());
        for (CombatHeavyUser combatHeavyUser : combatHeavyUsers) {
            checkHeavyUser(combatHeavyUser);
        }
        return 0;
    }

    /**
     * 删除重保用户信息
     *
     * @param id 重保用户主键
     * @return 结果
     */
    @Override
    public int deleteCombatHeavyUserById(Long id) {
        return combatHeavyUserMapper.deleteCombatHeavyUserById(id);
    }
}
